package ListNode;

import oracle.jrockit.jfr.StringConstantPool;

import java.util.HashSet;
import java.util.Set;

public class ListNode {

    public int val;
   public ListNode next;
    public ListNode(int val){
        this.val=val;
    }
    public ListNode(){

    }

    public ListNode revertListNode(ListNode head){
        ListNode preNode=null;
        ListNode nextNode=null;
        ListNode node=head;
        while(node!=null){
            ListNode newNode=node.next;
            node.next=nextNode;
            if(newNode==null){
                preNode=node;
            }
            nextNode=node;
            node=newNode;
        }
        return preNode;
    }

    public ListNode reverListNode2(ListNode head){
        ListNode preNode=null;
        ListNode nextNode=null;
        ListNode node=head;
        while(node!=null){
            ListNode newNode=node.next;
            node.next=newNode;
            if(newNode==null)
                preNode=node;
            nextNode=node;
            node=newNode;
        }
        return preNode;
    }

    public ListNode getHuan(ListNode head){
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(slow==fast){
                ListNode slow2=head;
                while(slow!=slow2){
                    slow=slow.next;
                    slow2=slow2.next;
                }
                return slow;
            }
        }
        return null;
    }

    public ListNode getHuan2(ListNode head){
        Set<Integer> set=new HashSet<Integer>();
        ListNode node=head;
        while(node!=null){
            if(!set.add(node.val)){
                return node;
            }
            node=node.next;


        }
        return null;
    }

    //删除倒数第n个节点
    public ListNode deleteListNode(ListNode head,int n){
        ListNode node = head;
        ListNode result = head;
        ListNode begin = result;
        int size = 0;
        while (node != null) {
            size ++;
            node = node.next;
        }
        size = size - n;
        if (size == 0) {
            result = result.next;
            return result;
        }
        for (int i = 0; i < size-1; i ++) {
            result = result.next;
        }
        result.next = result.next.next;
        return begin;
    }
//字符串加法
    public String sumString(String s,String t){
        StringBuilder sb=new StringBuilder();
        int i=s.length()-1;
        int j=t.length()-1;
        int carry=0;
        while(i>0||j>0||carry>0){
            int x=i<0?0:s.charAt(i--)-'0';
            int y=j<0?0:s.charAt(j--)-'0';
            int sum=x+y+carry;
            sb.append(sum%10);
            carry=sum/10;

        }
        return sb.reverse().toString();

    }
    //最大子集
    public String getMaxString(String str1,String str2){
        if(str1==null||str2==null){
            return null;
        }
        String res="";
        for(int i=0;i<str2.length();i++){
            if(!str1.contains(String.valueOf(str2.charAt(i)))){
                continue;
            }else{
                int left=i;
                int right=str2.length()-1;
                while(left<right){
                    String temp=str2.substring(left,right);
                    if(str1.contains(temp)){
                        res=res.length()>temp.length()?res:temp;
                        break;
                    }else{
                        right--;
                    }
                }

            }
        }
        return res;
    }
    public static  void main(String[]  args){
       /* ListNode node=new ListNode(1);
        node.next=new ListNode(2);
        node.next.next=new ListNode(3);
        ListNode resNode=new ListNode().revertListNode(node);
        while(resNode!=null){
            System.out.println(resNode.val);
            resNode=resNode.next;if(head == null){
            return null;
        }
        ListNode p = head;
        ListNode q = head;
        ListNode pre = null;
        while(n>0){
            if(p!=null){
                p = p.next;
            }else{
                return null;
            }
            n--;
        }
        if(p==null){
            return head.next;
        }
        while(p != null){
            p = p.next;
            pre = q ;
            q = q.next;
            if(p ==null){
                pre.next = q.next;
            }

        }
        return head;
        }*/
       ListNode node1=new ListNode(1);
       ListNode node2=new ListNode(2);
       ListNode node3=new ListNode(3);
       node1.next=node2;
       node2.next=node3;
       node3.next=node1;
      /*//* ListNode node=new ListNode().getHuan(node1);
       System.out.print(node.val+" "+node.next.val+" "+node.next.next.val+" "+node.next.next.next.val);*//*
       ListNode node=new ListNode().getHuan2(node1);
        System.out.print(node.val+" "+node.next.val+" "+node.next.next.val+" "+node.next.next.next.val);*/
      /*  ListNode res=node1.deleteListNode(node1,2);
        while(res!=null){
            System.out.println(res.val+" ");
            res=res.next;
        }*/

      String str1="1AB23456CD";
      String str2="123456EF";
      String res=node1.getMaxString(str1,str2);

      System.out.println(res);
    }
}
